/*
 * @Title Javaio.java
 * @Description
 * @Copyright Copyright(c) 2018
 * @Company com.lizhen
 * @author lizhen
 * @version 0.0.1
 */
package com.lizhen.core.function.io;

import java.io.*;

/**
 * @author lizhen
 */
public class IODemo {

    private static final String FILE_PATH = "1.8\\api\\main\\resource\\com\\lizhen\\core\\function\\io\\IODemo\\IODemo.txt";

    /**
     * @author lizhen
     */
    public IODemo() {
    }

    public static void Main(String[] args) {
        IODemo ioDemo = new IODemo();
        ioDemo.available();
        ioDemo.mark();
        ioDemo.markSupported();
    }

    /**
     * @author lizhen
     */
    public static void main(String[] args) {
        IOFile.main(null);
        IOFileListRoots.main(null);
        IOFileListFiles.main(null);
        IOFileFileFilter.main(null);
        IODemo.Main(null);
    }

    private void available() {
        InputStream inStream = null;
        BufferedInputStream bis = null;
        try {
            inStream = new FileInputStream(FILE_PATH);
            bis = new BufferedInputStream(inStream);
            while (bis.available() > 0) {
                Integer nBytes = bis.available();
                char ch = (char) bis.read();

                System.out.println("Available bytes = " + nBytes);
                System.out.println("The character read = " + ch);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void mark() {
        InputStream iStream = null;
        BufferedInputStream bis = null;
        try {
            iStream = new FileInputStream(FILE_PATH);
            bis = new BufferedInputStream(iStream);

            System.out.println("Char : " + (char) bis.read());
            System.out.println("Char : " + (char) bis.read());

            bis.mark(0);

            System.out.println("Char : " + (char) bis.read());
            System.out.println("reset() invoked");

            bis.reset();

            System.out.println("char : " + (char) bis.read());
            System.out.println("char : " + (char) bis.read());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (iStream != null) {
                try {
                    iStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void markSupported() {
        InputStream inStream = null;
        BufferedInputStream bis = null;
        boolean bool = false;
        try {
            inStream = new FileInputStream(FILE_PATH);
            bis = new BufferedInputStream(inStream);
            bool = bis.markSupported();

            System.out.println("Support for mark() and reset() : " + bool);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inStream != null) {
                try {
                    inStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static class IOFile {

        public static void printFilePath(String pathname) {
            File f = new File(pathname);

            System.out.println("File  Name: " + f.getName());
            System.out.println("File  exists: " + f.exists());
            System.out.println("Absolute Path: " + f.getAbsolutePath());
            try {
                System.out.println("Canonical Path: " + f.getCanonicalPath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static void main(String[] args) {
            printFilePath(FILE_PATH);
        }
    }

    static class IOFileListRoots {
        public static void main(String[] args) {
            File[] roots = File.listRoots();

            System.out.println("List of root directories:");
            for (File f : roots) {
                System.out.println(f.getPath());
            }
        }
    }

    static class IOFileListFiles {
        public static void main(String[] args) {
            String dirPath = "C:\\";
            File dir = new File(dirPath);
            File[] list = dir.listFiles();
            if (list != null) {
                for (File f : list) {
                    if (f.isFile()) {

                        System.out.println(f.getPath() + "  (File)");
                    } else if (f.isDirectory()) {

                        System.out.println(f.getPath() + "  (Directory)");
                    }
                }
            }
        }
    }

    static class IOFileFileFilter {
        public static void main(String[] args) {
            String dirPath = "C:\\";
            File dir = new File(dirPath);
            FileFilter filter = file -> {
                if (file.isFile()) {
                    String fileName = file.getName().toLowerCase();
                    if (fileName.endsWith(".sys")) {
                        return false;
                    }
                }
                return true;
            };
            File[] list = dir.listFiles(filter);
            if (list != null) {
                for (File f : list) {
                    if (f.isFile()) {

                        System.out.println(f.getPath() + "  (File)");
                    } else if (f.isDirectory()) {

                        System.out.println(f.getPath() + "  (Directory)");
                    }
                }
            }
        }
    }
}